package com.xiangxiao.rpan.storage.fastdfs;

import com.github.tobato.fastdfs.domain.conn.ConnectionPoolConfig;
import com.github.tobato.fastdfs.domain.conn.FdfsConnectionPool;
import com.github.tobato.fastdfs.domain.conn.PooledConnectionFactory;
import com.github.tobato.fastdfs.domain.conn.TrackerConnectionManager;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName FastdfsStorageConfig
 * @Auther admin
 * @Email 573768011@qq.com
 * @Date 2024/8/2 10:45
 */
@SpringBootConfiguration
@ComponentScan(value = {"com.github.tobato.fastdfs.service", "com.github.tobato.fastdfs.domain"})
// 解决jmx 重复注册bean
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@ConfigurationProperties(prefix = "rpan.storage.fastdfs") /** 该注解在多线程环境中能够正常获取配置文件application.yml中的值 */
@ConditionalOnProperty(prefix = "rpan.storage.processor", name = "type", havingValue = "com.xiangxiao.rpan.storage.fastdfs.FastDFSStorageProcessor")
public class FastdfsStorageConfig {
  private final String DEFAULT_ROOT_FILE_NAME = "rpan";
  private final String DEFAULT_GROUP = "XX";

  private Integer connectTimeout = 600;

  private List<String> trackerList = Lists.newArrayList();

  private String group = DEFAULT_GROUP;

  /**
   * 实际存放路径前缀
   */
  private String rootFilePath = System.getProperty("user.home") + File.separator + DEFAULT_ROOT_FILE_NAME + File.separator + "fastdfs";

  /**
   * 临时分片文件存放路径前缀
   */

  public FastdfsStorageConfig() {
  }

  public Integer getConnectTimeout() {
    return connectTimeout;
  }

  public void setConnectTimeout(Integer connectTimeout) {
    this.connectTimeout = connectTimeout;
  }

  public List<String> getTrackerList() {
    return trackerList;
  }

  public void setTrackerList(List<String> trackerList) {
    this.trackerList = trackerList;
  }

  public String getGroup() {
    return group;
  }

  public void setGroup(String group) {
    this.group = group;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    FastdfsStorageConfig that = (FastdfsStorageConfig) o;
    return Objects.equals(connectTimeout, that.connectTimeout) &&
        Objects.equals(trackerList, that.trackerList) &&
        Objects.equals(group, that.group);
  }

  @Override
  public int hashCode() {
    return Objects.hash(connectTimeout, trackerList, group);
  }

  @Override
  public String toString() {
    return "FastDFSClientConfig{" +
        "connectTimeout=" + connectTimeout +
        ", trackerList=" + trackerList +
        ", group='" + group + '\'' +
        '}';
  }

//  @Bean("connectionPoolConfig")
//  public ConnectionPoolConfig connectionPoolConfig() {
//    ConnectionPoolConfig connectionPoolConfig = new ConnectionPoolConfig();
//    return connectionPoolConfig;
//  }
//
//  @Bean("pooledConnectionFactory")
//  public PooledConnectionFactory pooledConnectionFactory() {
//    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
//    pooledConnectionFactory.setConnectTimeout(connectTimeout);
//    return pooledConnectionFactory;
//  }
//
//  @Bean(value = "fdfsConnectionPool")
//  public FdfsConnectionPool fdfsConnectionPool(ConnectionPoolConfig connectionPoolConfig, PooledConnectionFactory pooledConnectionFactory) {
//    FdfsConnectionPool fdfsConnectionPool = new FdfsConnectionPool(pooledConnectionFactory, connectionPoolConfig);
//    return fdfsConnectionPool;
//  }
//
//  @Bean(value = "trackerConnectionManager")
//  public TrackerConnectionManager trackerConnectionManager(FdfsConnectionPool fdfsConnectionPool) {
//    TrackerConnectionManager trackerConnectionManager = new TrackerConnectionManager(fdfsConnectionPool);
//    if (CollectionUtils.isEmpty(trackerList)) {
//      throw new RuntimeException("the tracker list can not be empty! <rpan.storage.fdfs.tracker-list>");
//    }
//    trackerConnectionManager.setTrackerList(trackerList);
//    return trackerConnectionManager;
//  }
}
